package edu.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 常用方法
 * setCharacterEncoding( String value ) : 设置指定的编码
 * setHeader( String name , String value ) : 设置已经存在的响应头
 * addHeader( String name , String value ) : 添加响应头 ，可以是不存在响应头
 * setContentType( String value ) : 设置 ContentType 的值。
 * setStatus( int sc ) : 设置响应状态
 * sendError( int sc , String msg ) : 发送错误
 * sendError( int sc ) : 发送错误
 */
@WebServlet("/responseDemo01")
public class ResponseDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        //response.setHeader("Content-Type","xxx");//Content-Type: xxx
        response.addHeader("MyHeader","testHeader");//MyHeader: testHeader
        response.setContentType("text/html; charset=UTF-8");  //Content-Type: gb2312
        response.setDateHeader("date",1000);    //Date: Thu, 01 Jan 1970 00:00:01 GMT

        PrintWriter writer = response.getWriter();
        writer.write("<h1>响应</h1>");
        //response.setStatus(200);
        //response.sendError(404,"禁止访问！！！"); //中文乱码
    }
}
